package com.apcc4m.sfpushbatch.common;

import java.io.Serializable;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PushMessage implements Serializable {

    public static final int MNG_PUSH_ALL = 2001;
    public static final int MNG_PUSH_SINGLE = 2002;
    public static final int MNG_PUSH_SPECIFY = 2003;

    private String msgId;
    private int cmd;
    private String fromId;
    private JSONArray toIds;
    private String title;
    private String content;
    private String time;
    private static long serial = 0;

    private static final long serialVersionUID = 6102678755781010665L;

    synchronized static long getSerial() {
        return (System.currentTimeMillis() >> 10) + serial++;
    }

    public PushMessage(String str) {
        try {
            JSONObject obj = new JSONObject(str);
            if (obj.has("msg")) {
                JSONObject jsonMsg = obj.getJSONObject("msg");
                if (jsonMsg.has("msgId")) {
                    this.setMsgId(jsonMsg.getString("msgId"));
                }

                if (jsonMsg.has("cmd")) {
                    this.setCmd(jsonMsg.getInt("cmd"));
                }

                if (jsonMsg.has("fromId")) {
                    this.setFromId(jsonMsg.getString("fromId"));
                }

                if (jsonMsg.has("toIds")) {
                    this.setToIds(jsonMsg.getJSONArray("toIds"));
                }
                if (jsonMsg.has("title")) {
                    this.setTitle(jsonMsg.getString("title"));
                }
                if (jsonMsg.has("content")) {
                    this.setContent(jsonMsg.getString("content"));
                }
            }
        } catch (Exception e) {
        }
    }

    public PushMessage(JSONObject obj) {
        try {
            if (obj.has("msg")) {
                JSONObject jsonMsg = obj.getJSONObject("msg");
                if (jsonMsg.has("msgId")) {
                    this.setMsgId(jsonMsg.getString("msgId"));
                }

                if (jsonMsg.has("cmd")) {
                    this.setCmd(jsonMsg.getInt("cmd"));
                }

                if (jsonMsg.has("fromId")) {
                    this.setFromId(jsonMsg.getString("fromId"));
                }

                if (jsonMsg.has("toIds")) {
                    this.setToIds(jsonMsg.getJSONArray("toIds"));
                }
                if (jsonMsg.has("title")) {
                    this.setContent(jsonMsg.getString("title"));
                }
                if (jsonMsg.has("content")) {
                    this.setContent(jsonMsg.getString("content"));
                }
            }
        } catch (Exception e) {
        }
    }

    public JSONObject toJson() {
        JSONObject jsonObj = null;
        try {
            JSONObject jsonMsg = new JSONObject();
            jsonMsg.put("msgId", this.getMsgId());
            jsonMsg.put("cmd", this.getCmd());
            jsonMsg.put("fromId", this.getFromId());
            jsonMsg.put("toIds", this.getToIds());
            jsonMsg.put("content", this.getContent());
            jsonMsg.put("title", this.getTitle());
            jsonMsg.put("time", this.getTime());
            jsonObj = new JSONObject();
            jsonObj.put("msg", jsonMsg);
        } catch (JSONException e) {
        }
        return jsonObj;
    }

    public String toString() {
        return this.toJson().toString();
    }

    public PushMessage() {
        msgId = getSerial() + "";
        time = new Date().toString();
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getMsgId() {
        return msgId;
    }

    public void setMsgId(String msgId) {
        this.msgId = msgId;
    }

    public String getFromId() {
        return fromId;
    }

    public void setFromId(String fromId) {
        this.fromId = fromId;
    }

    public JSONArray getToIds() {
        return toIds;
    }

    public void setToIds(JSONArray toIds) {
        this.toIds = toIds;
    }

}
